package com.binguo.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.binguo.api.form.YPCardRechargeForm;
import com.binguo.api.form.YPCardRegisterForm;
import com.binguo.api.result.Result;
import com.binguo.api.result.ResultGenerator;
import com.binguo.service.YpCardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

/**
 * @author yuritian
 * @Title: YPCardController
 * @ProjectName binguoDevelop
 * @Description: TODO
 * @date 2020-01-0914:35
 */
@Api(tags = "云澎卡绑定")
@RestController
@RequestMapping
public class YPCardController {

    @Autowired
    private YpCardService ypCardService;


    @PostMapping(value = "/api/ypCard/register", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "预登记云澎卡")
    public Result YPCardRegister(@RequestBody @Valid YPCardRegisterForm form) {
        ypCardService.registerYpCard(form.getShopId(),form.getCardId(),form.getCardNo());

        return ResultGenerator.genSuccessResult("success");
    }

    @PostMapping(value = "/api/ypCard/check", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "预登记云澎卡")
    public Result YPCardCheck(@RequestBody @Valid YPCardRegisterForm form) {
        String cardNo = ypCardService.getCardNo(form.getCardId(),form.getShopId());
        return ResultGenerator.genSuccessResult(cardNo);
    }

    @GetMapping(value = "/api/ypCard/getCardList/{shopId}/{pageNum}")
    @ApiOperation(value = "预登记云澎卡")
    public Result getCardList(@PathVariable Integer shopId,
                              @PathVariable Integer pageNum){

        JSONObject result = ypCardService.getYpCardList(shopId,pageNum*20);
        result.put("page",pageNum);

        return ResultGenerator.genSuccessResult(result);
    }

    @PostMapping(value = "/api/ypCard/modify")
    @ApiOperation(value = "预登记云澎卡")
    public Result modify(@RequestBody @Valid YPCardRegisterForm form){

        ypCardService.modifyYpCard(form.getShopId(),form.getCardId(),form.getCardNo());

        return ResultGenerator.genSuccessResult();
    }

    @PostMapping(value = "/api/ypCard/checkCard")
    @ApiOperation(value = "预登记云澎卡")
    public Result checkCard(@RequestBody @Valid YPCardRegisterForm form){

        ypCardService.checkYpCard(form.getShopId(),form.getCardId(),form.getCardNo());

        return ResultGenerator.genSuccessResult();
    }

    @GetMapping(value = "/api/ypCard/cardSum/{shopId}")
    @ApiOperation(value = "预登记云澎卡")
    public Result cardSum(@PathVariable Integer shopId){

        return ResultGenerator.genSuccessResult(ypCardService.getYpCardCountByShopId(shopId));
    }


    @PostMapping(value = "/api/ypCard/findCard")
    @ApiOperation(value = "预登记云澎卡")
    public Result findCard(@RequestBody @Valid YPCardRegisterForm form){

        return ResultGenerator.genSuccessResult(ypCardService.getYpCardMsg(form.getCardId(),form.getShopId()));
    }


    @PostMapping(value = "/api/ypCard/recharge")
    @ApiOperation(value = "预登记云澎卡")
    public Result recharge(@RequestBody @Valid YPCardRechargeForm form){

        ypCardService.rechargeYpCard(form.getCardNo(),form.getShopId(),form.getRechargeMoney());

        return ResultGenerator.genSuccessResult();
    }

}
